﻿@model IRuleExpression

@{
    var templateSelector = Resolve<IRuleTemplateSelector>();
    var templateInfo = templateSelector.GetTemplate(Model.Descriptor);
    var error = Model.Metadata.TryGetValue("Error", out var errorObj) ? errorObj.ToString() : string.Empty;
}

<div class="rule-row">
    <div class="rule row gx-1 gx-md-2 gx-lg-3" data-rule-id="@Model.Id" data-has-error="@error.HasValue().ToString().ToLower()">
        <div class="col-2">
            <partial name="_Rule.Descriptor.cshtml" model="Model" />
        </div>
        <div class="col-2">
            <partial name="_Rule.Operator.cshtml" model="Model" />
        </div>
        <div class="col-8">
            <div class="row flex-nowrap g-2">
                <div class="col">
                    @{
                        try
                        {
                            <partial name="@templateInfo.TemplateName.EnsureEndsWith(".cshtml")" model="Model" />
                        }
                        catch (Exception ex)
                        {
                            <div class="alert alert-danger mb-0 py-2" title="@ex.Message">
                                @T("Common.CannotDisplayView", templateInfo.TemplateName.NaIfEmpty())
                                @(ex.Message.Truncate(300, "…"))
                            </div>
                        }
                    }
                </div>

                <div class="col-auto">
                    <button class="btn btn-light btn-to-danger btn-icon r-delete-rule" type="button" tabindex="-1">
                        <i class="far fa-trash-can"></i>
                    </button>
                </div>
            </div>
            <div class="row flex-nowrap r-rule-error" attr-class='(error.IsEmpty(), "hide")'>
                <div class="col">
                    <span class="field-validation-error">@error</span>
                </div>
            </div>
        </div>
    </div>
</div>